<template>
  <div class="articleltem">
    <van-cell :to="`/article?id=${obj.art_id}`">
      <template #title>
        <div class="title-box">
          <span class="custom-title">{{ obj.title }}</span>
          <img
            class="thumb"
            v-if="obj.cover.type === 1"
            v-lazy="obj.cover.images[0]"
          />
        </div>
        <div class="thumb-box" v-if="obj.cover.type === 3">
          <img
            class="thumb"
            v-for="(item, i) in obj.cover.images"
            :key="i"
            v-lazy="item"
          />
        </div>
      </template>

      <template #label>
        <div class="label-box">
          <div>
            <span>{{ obj.aut_name }}</span>
            <span>{{ obj.comm_count }}评论</span>
            <span>{{ timeAgo(obj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->

          <van-icon name="cross" @click.stop="show = true" v-show="isShow" />
          <!-- 一级反馈 -->
          <van-action-sheet
            v-model="show"
            :actions="actions1"
            cancel-text="取消"
            @select="onSelect"
            get-container="body"
          />
          <!-- 二级反馈 -->
          <van-action-sheet
            v-model="shows"
            :actions="actions2"
            cancel-text="返回"
            @select="onSelect"
            get-container="body"
          />
        </div>
      </template>
    </van-cell>
  </div>
</template>

<script>
import { dislikeArticleApi, reportArticleApi } from "@/api/article";
export default {
  name: "ArticleItem",
  props: {
    obj: {
      type: Object,
      required: true,
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      shows: false,
      show: false,
      actions1: [
        { id: 1, name: "不感兴趣" },
        { id: 2, name: "反馈垃圾内容" },
      ],
      actions2: [
        { name: "其它问题" },
        { name: "标题夸张" },
        { name: "低俗色情" },
        { name: "错别字多" },
        { name: "旧闻重复" },
        { name: "内容不实" },
        { name: "侵权" },
        { name: "广告软文" },
        { name: "涉嫌违法犯罪" },
      ],
    };
  },
  methods: {
    async onSelect({ id }, index) {
      if (id === 1) {
        try {
          await dislikeArticleApi(this.obj.art_id);
          // 获取列表在父组件里 需要子传父
          this.$emit("dislike", this.obj.art_id);
          this.$toast.success("反馈成功");
        } catch (e) {
          this.$toast.fail("反馈失败");
        }
      } else if (id === 2) {
        this.shows = true;
      } else {
        try {
          await reportArticleApi(this.obj.art_id, index, "");
          this.$toast.success("举报成功");
        } catch (e) {
          this.$toast.fail("举报失败");
        } finally {
          this.shows = this.show = false;
        }
      }
    },
  },
};
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>